package com.jzy.stock.warehouse.controller;

import com.jzy.stock.util.ResultBuildVo;
import com.jzy.stock.util.ResultVo;
import com.jzy.stock.warehouse.model.AddVo;
import com.jzy.stock.warehouse.model.SelectVo;
import com.jzy.stock.warehouse.model.UpdateStatusVo;
import com.jzy.stock.warehouse.model.UpdateVo;
import com.jzy.stock.warehouse.service.WareHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * Created by JZZK on 2022/7/11.
 */
@RestController
@CrossOrigin
public class WareHouseController {

    @Autowired
    private WareHouseService wareHouseService;

    //分页查询
    @PostMapping("pickingArea/queryPage")
    public ResultVo queryPage(@RequestBody SelectVo selectVo) {
        return wareHouseService.queryPage(selectVo);
    }

    //按钮修改小类状态
    @PostMapping("pickingArea/updateStatus")
    public ResultVo updateStatus(@RequestBody @Valid UpdateStatusVo UpdateStatusVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return wareHouseService.updateStatus(UpdateStatusVo);
    }

    //新增
    @PostMapping("pickingArea/add")
    public ResultVo add(@RequestBody @Valid AddVo addVo, BindingResult bindingResult) {
        // 添加需要表单验证，如果验证不通过，直接把错误信息传给前端
        if (bindingResult.hasErrors()) {
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        ResultVo resultVo = wareHouseService.add(addVo);
        return resultVo;
    }

    //新增
    @PostMapping("pickingArea/update")
    public ResultVo add(@RequestBody @Valid UpdateVo updateVo, BindingResult bindingResult) {
        // 添加需要表单验证，如果验证不通过，直接把错误信息传给前端
        if (bindingResult.hasErrors()) {
            return ResultBuildVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        ResultVo resultVo = wareHouseService.update(updateVo);
        return resultVo;
    }
}
